package com.antunnel.common.orm;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.antunnel.basicorm.core.BaseDao;
import com.antunnel.basicorm.core.BaseService;
import com.antunnel.basicorm.widget.pagination.Page;

 
@Service
@Transactional
public class BaseServiceImpl implements BaseService{

	@Autowired
	@Qualifier("baseDaoImpl")
	private BaseDao baseDao = null;

	/**
	 * 获取实体类类型
	 * 
	 * @return
	 */
	private Class getClazz() {
		Class clazz = null;

		String className = this.getClass().getName()
				.replace("service.impl", "entity");

		className = className.substring(0, className.length() - 11);
		try {
			clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clazz;
	}

	/**
	 * 根据ID获取对象
	 * 
	 * @param id
	 *            对象ID
	 * @return 对象
	 */
	public Object findById(Serializable id) {
		return baseDao.findById(getClazz(), id);
	}



	/**
	 * 保存实体类
	 * 
	 * @param baseEntity
	 */
	public void save(Object baseEntity) {
		baseDao.save(baseEntity);
	}

	/**
	 * 修改实体类
	 * 
	 * @param baseEntity
	 */
	public Object update(Object baseEntity) {
		return baseDao.update(baseEntity);
	}

	/**
	 * 删除实体类
	 * 
	 * @param ids
	 */
	public void deleteIds(String ids) {
		String[] idArray = ids.split(",");
		for (int i = 0; i < idArray.length; i++) {
			baseDao.delete(getClazz(), new Integer(idArray[i]));
		}
	}
	
	public List findAll(Class clazz, String orderProperty, Boolean isAsc) {
		return baseDao.findAll(clazz, orderProperty, isAsc);
	}

	/**
	 * 分页获取对象集合
	 * 
	 * @param page
	 * @param searchMap
	 * @return
	 */
	public List findByPage(Page page, Map searchMap) {
		return baseDao.findByPage(getClazz(), page, searchMap);
	}
}
